import React, { Component } from 'react';
import { Route, Redirect, Switch } from 'react-router-dom';

import Home from '../pages/home/Home';
import Statistics from '../pages/statistics/Statistics';
import Permision from '../pages/permision/Permision';
import Userlist from '../pages/permision/Userlist';
import Setting from '../pages/setting/Setting';
import Settinguser from '../pages/setting/Settinguser';

export default class CRouter extends Component {
  requireAuth = (permission, component) => {
    const { auth } = this.props;
    const { permissions } = auth.data;
    if (!permissions || !permissions.includes(permission)) return <Redirect to={'404'} />;
    return component;
  };
  requireLogin = (component, permission) => {
    const { auth } = this.props;
    const { permissions } = auth.data;
    if (process.env.NODE_ENV === 'production' && !permissions) { // 线上环境判断是否登录
      return <Redirect to={'/login'} />;
    }
    return permission ? this.requireAuth(permission, component) : component;
  };
  render() {
    return (
      <Switch>
        <Route exact path="/admin/index" component={Home} />
        <Route exact path="/admin/statistics/tableAnalysis" component={Statistics} />
        <Route exact path="/admin/permision/role" component={Permision} />
        <Route exact path="/admin/user/List" component={Userlist} />
        <Route exact path="/admin/setting/list" component={Setting} />
        <Route exact path="/admin/setting/user" component={Settinguser} />
        <Route render={() => <Redirect to="/404" />} />
      </Switch>
    )
  }
}